<?php

/**
 * Controller_Default
 */


class Controller_default extends MyApp
{

	function __construct()
	{
		parent::__construct();
		$this->model->user = YOU::loadModel('user');
		$this->model->notice = YOU::loadModel('notice');
		$this->model->inform = YOU::loadModel('inform');
		$this->model->semester = YOU::loadModel('semester');

	}

	/** 首页 - 综合数据**/
	function actionIndex()
	{
		$user_id = $this->user['user_id'];
		$type_id = $this->user['type_id'];
		if ($user_id) {
			//底部  通用事项通知未读取数量

			$read_lists = $this->db->fetchAll("SELECT * FROM `tbl_inform_read` WHERE read_to= {$user_id} ORDER BY read_id ");
			if ($read_lists) {
				$inform = array();
				foreach ($read_lists as $key => $value) {
					$inform[$key] = $value['inform_id'];
				}
				$informs = implode(',', $inform);//可以看到的信息的ID
				$canread = 'inform_id in (' . $informs . ')';
			} else {
				$canread = 'inform_id in (0)';
			}
			$read_lists = $this->db->fetchAll("SELECT * FROM `tbl_notice_read` WHERE read_to= {$user_id} ORDER BY read_id ");
			if ($read_lists) {
				$inform = array();
				foreach ($read_lists as $key => $value) {
					$inform[$key] = $value['notice_id'];
				}
				$informs = implode(',', $inform);//可以看到的信息的ID
				$cannotice = 'notice_id in (' . $informs . ')';
			} else {
				$cannotice = 'notice_id in (0)';
			}
			//echo $canread;
		} else {
			echo '错误：未登录！';
			exit;
		}

		$myname = $this->model->user->getNameById($user_id);

		$user_id = $this->user['user_id'];


		// 通知 条数5（调取的是校长室的通知，不需要审核,通用事项发布通知审核过的）

		$num = 5;
		$time = date('Y-m-d H:i',time());
		$inform_lists_one = $this->model->notice->getIndexTwo($user_id,$num,$time);
		$inform_content_one = $this->model->notice->getIndexTowCount($user_id,$time);
		foreach ($inform_lists_one as $key=>$value){
			// 控制标题字数
			if(strlen($value['notice_title'])>7){
				$inform_lists_one[$key]['notice_title']=mb_substr($value['notice_title'],0,7,utf8).'...';

			}
		}
		//print_R($inform_lists_one);
		// 公告 条数5
		//$inform_type=2;

		$notice_type = 1;
		$num = 5;
		$inform_lists_two = $this->model->notice->getIndexOne($user_id,$notice_type,$num);
		$inform_content_two = $this->model->notice->getIndexOneCount($user_id,$notice_type);
		foreach ($inform_lists_two as $key => $value){
			// 控制标题字数
			if(strlen($value['notice_title'])>7){
				$inform_lists_two[$key]['notice_title']=mb_substr($value['notice_title'],0,7,utf8).'...';

			}
		}
		// 通知审核
		$notice_type = 3;
		$inform_lists_three = $this->model->notice->getIndexThree($type_id,$notice_type,$num);
		$inform_content_three = $this->model->notice->getIndexThreeCount($type_id,$notice_type);
		if (empty($inform_content_three)) {

			$inform_content_three['count'] = 0;
		}

		if (!empty($inform_lists_three)) {
			foreach ($inform_lists_three as $key => $value){
				// 控制标题字数
				if(strlen($value['notice_title'])>7){
					$inform_lists_three[$key]['notice_title']=mb_substr($value['notice_title'],0,7,utf8).'...';

				}
			}
		}




		// 事务交代
		$notice_type = 5;
		$inform_type = 1;

		$one  = $this->model->notice->getIndexFour($user_id,$notice_type,$num);
		$one_cont = $this->model->notice->getIndexFourCount($user_id,$notice_type);

		// 上级通知
		$two = $this->model->inform->getIndexlist($user_id,$inform_type,$num);
		$two_cont = $this->model->inform->getIndexcount($user_id,$inform_type,$num);

		$inform_content_four = $one_cont['count']+$two_cont;
		$inform_lists_four= array_merge($one,$two);

		// 排序
		$sort = array(
		'direction' => 'SORT_DESC',			 //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
		'field'     => 'create_time',        //排序字段
		);
		$sorts = array(
		'no' 		=> 'SORT_ASC',			 //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
		'read'      => 'is_read',            //排序字段
		);
		$arrSort = array();
		foreach($inform_lists_four AS $uniqid => $row){
			foreach($row AS $key=>$value){
				$arrSort[$key][$uniqid] = $value;
			}
		}
		if($sorts['no']){
			@array_multisort($arrSort[$sort['read']], constant($sort['no']), $inform_lists_four);
		}
		if($sort['direction']){
			@array_multisort($arrSort[$sort['field']], constant($sort['direction']), $inform_lists_four);
		}

		// 显示5个
		foreach ($inform_lists_four as $key => $value){
			if ($key >=5) {

				unset($inform_lists_four[$key]);
			}

			// 控制标题字数
			if(strlen($value['notice_title'])>7){
				$inform_lists_four[$key]['notice_title']=mb_substr($value['notice_title'],0,7,utf8).'...';

			}
			// 控制标题字数
			if(strlen($value['inform_title'])>7){
				$inform_lists_four[$key]['inform_title']=mb_substr($value['inform_title'],0,7,utf8).'...';

			}

		}
		// 提醒

		$num  = 5;
		$one  = $this->model->notice->getIndexSix($user_id, $notice_type, $num);
		$one_cont  = $this->model->notice->getIndexSixCount($user_id, $notice_type, $num);
		// 上级通知
		$two = $this->model->inform->getIndexSixlist($user_id,$inform_type,$num);
		$two_cont = $this->model->inform->getIndexSixCount($user_id,$inform_type,$num);

		$inform_content   = $one_cont['count']+$two_cont['count'];
		$inform_lists     = array_merge($one,$two);

		// 排序
		$sort = array(
		'direction' => 'SORT_DESC',			 //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
		'field'     => 'create_time',        //排序字段
		);
		$sorts = array(
		'no' 		=> 'SORT_ASC',			 //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
		'read'      => 'is_read',            //排序字段
		);
		$arrSort = array();
		foreach($inform_lists AS $uniqid => $row){
			foreach($row AS $key=>$value){
				$arrSort[$key][$uniqid] = $value;
			}
		}
		if($sorts['no']){
			@array_multisort($arrSort[$sort['read']], constant($sort['no']), $inform_lists);
		}
		if($sort['direction']){
			@array_multisort($arrSort[$sort['field']], constant($sort['direction']), $inform_lists);
		}

		// 显示5个
		foreach ($inform_lists as $key => $value){
			if ($key >=5) {

				unset($inform_lists[$key]);
			}
			// 控制标题字数
			if(strlen($value['notice_title'])>7){
				$inform_lists[$key]['notice_title']=mb_substr($value['notice_title'],0,7,utf8).'...';

			}
		}

		// 内部邮件 条数
		$mail_lists = $this->db->fetchAll("SELECT * FROM `tbl_mail` WHERE mail_to='{$this->user['user_id']}' ORDER BY mail_id DESC LIMIT 0,5");
		$mail_content = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_mail` WHERE mail_to='{$this->user['user_id']}' AND is_read='0'");
		// 通知部门id
		foreach($mail_lists as $key => $value){
			$mail_lists[$key]['create_time'] =date('m-d H:i',strtotime($value['create_time']));
			// 发件人
			$staff_data = $this->db->fetchRow("SELECT * FROM `tbl_staff` WHERE staff_id={$value['mail_from']}");
			$mail_lists[$key]['staff_name'] = $staff_data['staff_name'];
			// 控制标题字数
			if(strlen($value['mail_title'])>7){
				$mail_lists[$key]['mail_title']=mb_substr($value['mail_title'],0,7,utf8).'...';

			}
		}


		$this->tpl->assign("students_lists", $students_lists);
		$this->tpl->assign("class_lists", $class_lists);

		$this->tpl->assign("main_lists", $main_lists);
		$this->tpl->assign("main_content", $main_content);

		$this->tpl->assign("shop_lists", $shop_lists);
		$this->tpl->assign("shop_content", $shop_content);

		$this->tpl->assign("goods_lists", $goods_lists);
		$this->tpl->assign("goods_content", $goods_content);

		$this->tpl->assign("inform_lists", $inform_lists);
		$this->tpl->assign("inform_content", $inform_content);

		$this->tpl->assign("inform_all_three", $inform_all_three);
		$this->tpl->assign("inform_all_three_count", $inform_all_three_count);

		$this->tpl->assign("inform_all_two", $inform_all_two);
		$this->tpl->assign("inform_all_two_count", $inform_all_two_count);

		$this->tpl->assign("inform_lists_three", $inform_lists_three);
		$this->tpl->assign("inform_content_three", $inform_content_three);

		$this->tpl->assign("inform_lists_two", $inform_lists_two);
		$this->tpl->assign("inform_content_two", $inform_content_two);

		$this->tpl->assign("inform_lists_one", $inform_lists_one);
		$this->tpl->assign("inform_content_one", $inform_content_one);

		$this->tpl->assign("inform_lists_four", $inform_lists_four);
		$this->tpl->assign("inform_content_four", $inform_content_four);

		$this->tpl->assign("mail_lists", $mail_lists);
		$this->tpl->assign("mail_content", $mail_content);

		$this->tpl->assign("work_one", $work_one);
		$this->tpl->assign("work_one_content", $work_one_content);

		$this->tpl->assign("work_two", $work_two);
		$this->tpl->assign("work_two_content", $work_two_content);

		$this->tpl->assign("work_three", $work_three);
		$this->tpl->assign("work_three_content", $work_three_content);

		$this->tpl->assign("plan_lists", $plan_lists);
		$this->tpl->assign("plan_content", $plan_content);

		$this->tpl->assign("work_lists", $work_lists);
		$this->tpl->assign("work_content", $work_content);

		$this->tpl->assign("arrange_lists", $arrange_lists);
		$this->tpl->assign("arrange_content", $arrange_content);

		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("plan_week", $plan_week);
		$this->tpl->assign("time", $time);

		// 老师首页
		if ($this->user['user_type'] == 'staff') {
			$this->tpl->display('m.default.index.tpl.php');
		}
		// 管理员首页
		if ($this->user['user_type'] == 'admin') {
			$this->tpl->display('m.default.index.tpl.php');
		}
		// 家长首页
		if ($this->user['user_type'] == 'student') {
			$this->tpl->display('m.student.index.tpl.php');
		}
		// 维修首页
		if ($this->user['user_type'] == 'repair') {

			// 结算类型
			$is_shows = $this->is_show;

			// 维修单位
			$maince_lists = $this->db->fetchAll("SELECT * FROM `tbl_maince`");
			// 报修人
			$main_names = $this->db->fetchAll("SELECT distinct user_id FROM `tbl_main`");
			foreach ($main_names as $key => $value) {
				// 名字
				$main_names[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);

			}
			// 查询条件
			$main_name = $this->get->main_name;
			$maince_com = $this->get->maince_com;
			$from = $this->get->from;
			$to = $this->get->to;
			$is_show = $this->get->is_show;

			$where = " ";

			if (!empty($main_name)) {
				$staff_id = $this->model->user->getIdByName($main_name);

				// 申报人姓名
				$where .= " AND a.user_id LIKE '{$staff_id}'";
				$this->tpl->assign("main_name", $main_name);

			}
			if (!empty($maince_com)) {
				// 维修单位
				$where .= " AND b.maince_com LIKE '%$maince_com%'";
				$this->tpl->assign("maince_com", $maince_com);
			}
			if (!empty($from)) {
				// 维修完成开始时间
				$where .= " AND a.maintain_time >= '$from' ";
				$this->tpl->assign("from", $from);
			}
			if (!empty($to)) {
				// 维修完成结束时间
				$where .= " AND a.maintain_time <= '$to'";
				$this->tpl->assign("to", $to);
			}
			if (!empty($is_show)) {
				// 维修完成结束时间
				$where .= " AND a.is_show = '$is_show'";
				$this->tpl->assign("is_show", $is_show);
			}


			// 分页
			YOU::loadLib('page', "", '0');
			$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_main` AS a,`tbl_maince` AS b  WHERE a.maince_id= b.maince_id AND main_type='3' {$where}");
			$count = $tmp['count'];
			$page_total = ceil($count / PAGE_LIMIT);
			// 当前页
			$p = intval($_GET['p']);
			if ($p == 0) {
				$p = 1;
			}
			if ($p > 1) {
				$start = PAGE_LIMIT * ($p - 1);
			} else {
				$start = 0;
			}

			$main_lists = $this->db->fetchAll("SELECT * FROM `tbl_main` AS a,`tbl_maince` AS b  WHERE a.maince_id= b.maince_id {$where} AND main_type='3'  ORDER BY a.main_id DESC LIMIT {$start}, " . PAGE_LIMIT . "");

			foreach ($main_lists as $key => $value) {
				// 公司名称
				if (!empty($value['maince_id'])) {

					$maince_com = $this->db->fetchRow("SELECT `maince_com` FROM `tbl_maince` WHERE maince_id = {$value['maince_id']}");
					$main_lists[$key]['maince_com'] = $maince_com['maince_com'];
				}
				// 结算
				$main_lists[$key]['is_show'] = $this->is_show[$value['is_show']];
				// 提交者
				$main_lists[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);
			}

			$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
			$page->next_page = '下一页>';
			$page->pre_page = '<上一页';
			$page->style_path = "static/page_style";

			$this->tpl->assign("page", $page->show(4, "yahoo"));
			$this->tpl->assign("main_lists", $main_lists);
			$this->tpl->assign("maince_lists", $maince_lists);
			$this->tpl->assign("is_shows", $is_shows);

			$this->tpl->assign("main_names", $main_names);
			//$this->tpl->display('main/demand.list.tpl.php');
			$this->tpl->display('m.repair.index.tpl.php');

			//var_dump($this->user);

		}

	}
	/** 首页 - 校长室**/
	function actionPrincipal()
	{
		$this->tpl->display('m.principal.index.tpl.php');
	}

	/** 首页 - 教务处**/
	function actionJiaowu()
	{
		$this->tpl->display('m.jiaowu.index.tpl.php');
	}

	/** 首页 - 班主任**/
	function actionBanzhuren()
	{
		$this->tpl->display('m.banzhuren.index.tpl.php');
	}

	/** 首页 - 教研室**/
	function actionjiaoyanshi()
	{
		$this->tpl->display('m.jiaoyanshi.index.tpl.php');
	}

	function actionZhengjiao()
	{
		$this->tpl->display('m.index.zhengjiao.tpl.php');
	}

	function actionZongwu()
	{
		$this->tpl->display('m.index.zongwu.tpl.php');

	}

	function actionTongyong()
	{
		$this->tpl->display('m.index.tongyong.tpl.php');

	}

	function actionDeyu()
	{
		$this->tpl->display('m.index.deyu.tpl.php');

	}



}

?>